package com.jjy.admin.service.predict.impl;

import com.jjy.admin.domain.JobEntity;
import com.jjy.admin.domain.PredictSalaryDTO;
import com.jjy.admin.mapper.JobMapper;
import com.jjy.admin.service.predict.PredictService;
import org.springframework.stereotype.Service;
import weka.classifiers.functions.LinearRegression;
import weka.core.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
public  class PredictServiceImpl implements PredictService {
    @Resource
    private JobMapper jobMapper;

    @Override
    public String doPredictSalary(PredictSalaryDTO predictSalaryDTO) {
//        根据关键字获取数据集
        ArrayList<JobEntity> jobEntities = jobMapper.selectByKeyWord();
        // 创建空的Instances对象，并添加属性
        // 创建属性列表
        FastVector attributes = new FastVector();

        // 添加属性
        attributes.addElement(new Attribute("cityCode"));
        attributes.addElement(new Attribute("experience"));
        attributes.addElement(new Attribute("degree"));
        attributes.addElement(new Attribute("salary"));

        // 创建空的Instances对象
        Instances data = new Instances("Dataset", attributes, 0);
        // 遍历ResultSet并添加实例到data
        for (JobEntity jobEntity : jobEntities) {
            double[] vals = new double[4]; //
            vals[0] = Double.parseDouble(jobEntity.getCity());
            vals[1] = Double.parseDouble( "无".equals(jobEntity.getExperience())?"0": jobEntity.getExperience());
            vals[2] = Double.parseDouble(jobEntity.getDegree());
            Double salary = (Double.parseDouble(jobEntity.getJobSalaryMax())+Double.parseDouble(jobEntity.getJobSalaryMin()))/2;
            vals[3] = salary; // 薪资是标签
            Instance inst = new DenseInstance(1.0, vals); // 权重设为1.0，进行回归预测
            inst.setDataset(data); // 设置数据集
            data.add(inst);
        }
        // 设置类别索引（薪资）
        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }
        // 初始化并训练线性回归模型
        LinearRegression lr = new LinearRegression();
        double predictedSalary=0;
        ArrayList<Double> doubles = new ArrayList<>();
        if (predictSalaryDTO.getCityCode()!=null){
            doubles.add(Double.parseDouble(predictSalaryDTO.getCityCode()));
        }
        if (predictSalaryDTO.getExperience()!=null){
            doubles.add(Double.parseDouble(predictSalaryDTO.getExperience()));
        }
        if (predictSalaryDTO.getDegree()!=null){
            doubles.add(Double.parseDouble(predictSalaryDTO.getDegree()));
        }
        try {
            lr.buildClassifier(data);
            double[] featureValues = doubles.stream().mapToDouble(Double::doubleValue).toArray();
            Instance newInstance = new DenseInstance(featureValues.length);
            newInstance.setDataset(data); // 设置数据集以获取属性信息
            for (int i = 0; i < featureValues.length; i++) {
                newInstance.setValue(i, featureValues[i]);
            }

            // 使用模型进行预测
            predictedSalary= lr.classifyInstance(newInstance);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return String.valueOf(Math.round(predictedSalary));
    }
}
